<template>
  <div class="window">
    <span class="icon iconfont icon-delete windel" @click="callShowWindow()"></span>
    <div class="search-wrapper friendSearch">
      <input type="text" class="searchInput" v-model="keyword" placeholder="用户ID/手机号" />
      <span class="icon iconfont icon-sousuo"></span>
      <div class="search-btn" @click="searchFriend()">查询</div>
    </div>
    <div class="searchlist">
      <ul>
        <li v-for="(friend, key) in friendList" :key="key">
          <div class="listleft">
            <img :src="friend.img" alt class="avatar" />
          </div>
          <div class="listright">
            <p class="name">{{friend.nickname}}</p>
            <span class="add-btn" @click="selectFriend(friend.id)">添加</span>
            <div class="askmsg">{{friend.wxid}}</div>
          </div>
        </li>
      </ul>
    </div>
    <div class="addWindow" v-show="flag">
      <div class="add">
        <span class="addtitle">添加朋友</span>
        <span class="icon iconfont icon-delete adddel" @click="closeAddWindow()"></span>
        <textarea v-model="askMsg"></textarea>
        <p class="tip">你需要发送验证请求，对方通过后你才能添加其为朋友</p>
        <div class="fix-btn" @click="addFriend()">确定</div>
        <div class="cancel-btn" @click="closeAddWindow()">取消</div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { mapState } from "vuex";

export default {
  computed: {
    ...mapState(["user", "ip", "port"])
  },
  data() {
    return {
      keyword: "",
      friendList: [],
      flag: false,
      askMsg: "",
      selectFriendId: ""
    };
  },
  methods: {
    callShowWindow() {
      this.keyword = "";
      this.friendList = [];
      this.$emit("closeWindow");
    },
    searchFriend() {
      if (this.keyword.length == 0) {
        return;
      }
      axios
        .get(
          `http://${this.ip}:${this.port}/friend/searchFriend/${this.user.id}/${this.keyword}`
        )
        .then(res => {
          if (res.data.code == 200) {
            this.friendList = res.data.data;
          }
        });
    },
    closeAddWindow() {
      this.flag = false;
    },
    selectFriend(value) {
      this.askMsg = "我是" + this.user.nickname;
      this.selectFriendId = value;
      this.flag = true;
    },
    addFriend() {
      axios
        .post(`http://${this.ip}:${this.port}/friend/addRequest/`, {
          request_from: this.user.id,
          request_to: this.selectFriendId,
          request_msg: this.askMsg
        })
        .then(res => {
          if (res.data.code == 200) {
            this.askMsg = "";
            this.flag = false;
            this.callShowWindow();
          }
        });
    }
  }
};
</script>

<style scoped>
.window {
  position: absolute;
  top: 22px;
  right: 25px;
  width: 500px;
  height: 510px;
  background-color: #e6e6e6;
  z-index: 2;
}

.icon-delete {
  display: inline-block;
  position: absolute;
  right: 0;
  width: 24px;
  height: 27px;
  font-size: 17px;
  line-height: 27px;
  text-align: center;
  color: #cfcfcf;
}

.windel {
  top: 0;
  right: 0;
}

.search-wrapper {
  position: relative;
  display: flex;
  flex: 1;
  min-width: 0;
  height: 26px;
  box-sizing: border-box;
  border-radius: 5px;
  background-color: #e5e3e2;
}

.friendSearch {
  margin: 30px 40px;
  background-color: transparent;
}

.searchInput {
  flex: 1;
  min-width: 0;
  font-size: 12px;
  box-sizing: border-box;
  padding: 6px 0 6px 25px;
  border: 2px solid #dbd9d8;
  border-radius: 5px;
  background-color: #dbd9d8;
  outline: none;
}

.searchInput:focus {
  background-color: #f2efee;
}

.icon-sousuo {
  display: inline-block;
  position: absolute;
  width: 24px;
  height: 26px;
  font-size: 17px;
  line-height: 26px;
  text-align: center;
  color: #636363;
}

.search-btn {
  width: 80px;
  height: 24px;
  line-height: 24px;
  text-align: center;
  border-radius: 12px;
  background-color: #dbd9d8;
  margin-left: 20px;
  cursor: pointer;
}

.searchlist {
  padding: 0 50px;
}

.searchlist li {
  display: flex;
  height: 50px;
  padding: 15px 0;
}

.listleft {
  position: relative;
  margin-right: 15px;
}

.avatar {
  width: 50px;
  height: 50px;
  border-radius: 3px;
}

.listright {
  position: relative;
  flex: 1;
}

.name {
  display: inline-block;
  vertical-align: top;
  font-size: 14px;
}

.add-btn {
  position: absolute;
  display: inline-block;
  right: 0px;
  top: 15px;
  width: 60px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  border-radius: 10px;
  font-size: 14px;
  background-color: #dbd9d8;
  cursor: pointer;
}

.askmsg {
  position: absolute;
  font-size: 13px;
  width: 130px;
  height: 15px;
  line-height: 15px;
  color: #999;
  bottom: 3px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.addWindow {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.add {
  position: absolute;
  top: 100px;
  left: 65px;
  width: 360px;
  height: 200px;
  background-color: #f2f2f2;
}

.addtitle {
  position: relative;
  display: inline-block;
  padding: 5px 0 0 10px;
  color: #999999;
}

.add textarea {
  position: absolute;
  top: 45px;
  left: 10px;
  width: 332px;
  height: 80px;
  box-sizing: border-box;
  padding-top: 8px;
  border: 2px solid #dbd9d8;
  resize: none;
  outline: none;
}

.tip {
  position: absolute;
  top: 130px;
  left: 12px;
  font-size: 13px;
  color: #999999;
}

.fix-btn,
.cancel-btn {
  position: absolute;
  bottom: 10px;
  width: 60px;
  height: 25px;
  line-height: 25px;
  text-align: center;
  border-radius: 2px;
}

.fix-btn {
  right: 80px;
  color: #fff;
  background-color: #1aad19;
  cursor: pointer;
}

.cancel-btn {
  right: 12px;
  border-color: #e7e7e7;
  background-color: #fff;
  cursor: pointer;
}
</style>